/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Koala;

import client.Client;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;

/**
 *
 * @author Kam
 */
public class OkienkoGlowne extends javax.swing.JFrame {
    
    public Client program;
    public String logi;
    public String pass;
    public Contacts contacts;
    public Contacts_List list;
    /**
     * Creates new form OkienkoGlowne
     */
    public OkienkoGlowne(Client klient, String login, String password) {
        this.program = klient;
        this.logi = login;
        this.pass = password;
        this.contacts = new Contacts();
        new clientThread(this).start();
        try {
            this.program.importContactsFromServer();
        } catch (Exception ex) {
            Logger.getLogger(OkienkoGlowne.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
        panel.setBackground(Color.red);
       // getContentPane().add(new Contacts_List(this.program).userContacts);//
        list = new Contacts_List(this.program,panel, this);
       // panel.add(new Contacts_List(this.program).userContacts, BorderLayout.CENTER);
       // ziomek.setVisible(true);
        panel.setLayout(new BorderLayout());

        panel.setSize(500, 550);

        //list.refreshContactList(new Contacts().vec);
        //pane = new JScrollPane(new JList (vector));
        panel.add(list.userContacts);
        
        
       
       // setSize(ziomek);
        this.jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
    }
    
    private void setSize(JFrame kicius) {
	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	
	Dimension size = new Dimension(screenSize);//rozmiar ramki
	size.width = size.width / 6;
	size.height = (int) (size.height / 1.5);
	
	kicius.setLocation((int) ((screenSize.width / 2) - (size.width / 2)),
		(int) ((screenSize.height / 2) - (size.height / 2)));
	
	kicius.setSize(size);
	//setVisible(true);
	//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        addContactButton = new javax.swing.JToggleButton();
        logoutButton = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        panel = new javax.swing.JPanel();

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        addContactButton.setText("Dodaj kontakt");
        addContactButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addContactButtonActionPerformed(evt);
            }
        });

        logoutButton.setText("Wyloguj");
        logoutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutButtonActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Dostępny", "Niewidoczny", "Niedostępny" }));
        jComboBox1.setSelectedIndex(1);
        jComboBox1.setName("status"); // NOI18N
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 262, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 248, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(addContactButton, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(logoutButton, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addContactButton)
                    .addComponent(logoutButton))
                .addGap(13, 13, 13)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jComboBox1.getAccessibleContext().setAccessibleName("status");
        jComboBox1.getAccessibleContext().setAccessibleDescription("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void logoutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutButtonActionPerformed
        if (this.program.getCzyZalgowany() == true) {
            try {
                this.program.logoutFromServer();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        this.dispose();
    }//GEN-LAST:event_logoutButtonActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    }//GEN-LAST:event_formWindowClosed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        if (this.program.getCzyZalgowany() == true) {
            try {
                this.program.logoutFromServer();
                Thread.sleep(500);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        this.dispose();
    }//GEN-LAST:event_formWindowClosing

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
         int choosen = jComboBox1.getSelectedIndex();
         try {
         if( choosen == 0) {
             if (this.program.getCzyZalgowany() == false) {
                 this.program = new Client (this.program.getIp(), this.program.getPort());
                 this.program.loginToServer(logi, pass, null);
                 new clientThread(this).start();
             }
             program.changeStatus(utils.Status.AVALIBLE);
         }
         if( choosen == 1) {
             if (this.program.getCzyZalgowany() == false) {
                 this.program = new Client (this.program.getIp(), this.program.getPort());
                 this.program.loginToServer(logi, pass, null);
                 new clientThread(this).start();
             }
             program.changeStatus(utils.Status.INVISIBLE);
         }
         if( choosen == 2) {
             if (this.program.getCzyZalgowany() == true) {
                 this.program.logoutFromServer();
             }
         }
         } catch (Exception e) {
             System.out.print(e.getMessage());
         }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void addContactButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addContactButtonActionPerformed
        this.repaint();
        Add window = new Add(list,false);
        window.setVisible(true);
    }//GEN-LAST:event_addContactButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton addContactButton;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JButton logoutButton;
    private javax.swing.JPanel panel;
    // End of variables declaration//GEN-END:variables
}
